package net.lzzy.practices.services;

import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.Chapter;
import net.lzzy.practices.entities.Course;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.repositories.ChapterRepository;
import net.lzzy.practices.repositories.CourseRepository;
import net.lzzy.practices.utils.AuthUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author zly on 2022/5/30
 * Description:
 * <p>
 * 1.实现接口(ChapterService)
 * 2.用Services注释
 * 3.添加private字段(接口名)
 * 4.通过构造器注入 完成自动装配
 */
@Service
public class ChapterServiceImpl implements ChapterService {
    private final ChapterRepository repository;
    private final CourseRepository cRepository;


    @Autowired
    public ChapterServiceImpl(ChapterRepository repository, CourseRepository cRepository) {
        this.repository = repository;
        this.cRepository = cRepository;
    }

    @Override
    public List<Chapter> getTeachersChapters(UUID courseId, String token) {
        if(checkTeacher(courseId,token)==null){return null;}
        return repository.findChaptersByCourseIdOrderByUpdateTimeDesc(courseId);
    }
    private Course checkTeacher(UUID CourseId,String token){
        Course course=cRepository.findById(CourseId).orElse(null);
        if(course ==null){
            return null;
        }
        return course;
    }

    @Override
    public List<Chapter> getStudentsChapters(UUID courseId, String token) {
        Course course = cRepository.findById(courseId).orElse(null);
        if (course == null) {

            return null;
        }
        List<User> students = course.getStudents();
        UUID studentId = AuthUtils.getUser(token).getId();
        long count = students.stream().filter(s -> s.getId().equals(studentId)).count();
        if(count==0)
        {
            return null;
        }

        return repository.findChaptersByCourseIdAndOpenOrderByUpdateTimeDesc(courseId,true);
    }

    @Override
    public Chapter add(UUID courseId, Chapter chapter, String token) {
        Course course = checkTeacher(courseId,token);
        if(course==null){
            return null;
        }
        chapter.setCourse(course);
        return  repository.save(chapter);


    }

    @Override
    public Chapter update(UUID id, Chapter chapter, String token) {
        Chapter old=repository.findById(id).orElse(null);
        if(old==null){return null;}
        UUID teacherId = old.getCourse().getTeacher().getId();
        if (AuthUtils.invalid(teacherId,token)){
            return null;
        }
        old.setDescription(chapter.getDescription());
        old.setName(chapter.getName());
        old.setOpen(chapter.isOpen());
        old.setUpdateTime(new Date());
        return repository.save(old);
    }




    @Override
    public Chapter openCloseChapter(UUID id, boolean open, String token) {
        Chapter old = repository.findById(id).orElse(null);
        if(old !=null){
            old.setOpen(open);
        }
        return update(id,old,token);
    }

    @Override
    public Messages remove(UUID id, String token) {
        Chapter chapter = repository.findById(id).orElse(null);
        if (chapter == null) {
            return Messages.WRONG_ID;
        }
        UUID teacherId = chapter.getCourse().getTeacher().getId();
        if (AuthUtils.invalid(teacherId, token)) {
            return Messages.RELATED_DATA;

        }
        if (chapter.getQuestions().size() > 0) {
            return Messages.RELATED_DATA;
        }
        repository.delete(chapter);
        return Messages.SUCCESS;
    }
}
